#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Created by master on 2017/3/12

from twisted.internet import protocol
from twisted.internet import reactor


class Echo(protocol.Protocol):
    def dataReceived(self, data):  # 只要twisted一收到数据就会调用此方法

        self.transport.write(data)  # 然后把收到的数据返回
        print("received:", data)


def main():
    factory = protocol.ServerFactory()  # define a basic factory object
    factory.protocol = Echo  # socketserver 中的 handle

    reactor.listenTCP(9999, factory)  # 检测端口
    reactor.run()


if __name__ == '__main__':
    main()
